module executor.group.GroupInviteExecutor;

import msgtrans;
import message.Constants;
import cache.UserStatusManager;
import hunt.logging;
import google.protobuf;
import message.MsgDefine;
import client.LogicServerClient;
import client.GateServerClient;
import std.array;
import std.conv : to;

@TransportServer(MsgServerName)
class GroupInviteExecutor : AbstractExecutor!(GroupInviteExecutor) {

  @MessageId(MESSAGE.CMD_INVITE_GROUP)
  void groupInvite(TransportContext ctx, MessageBuffer buffer) {
    //InviteGroup inviteGroup = new InviteGroup;
    //buffer.data.fromProtobuf!InviteGroup(inviteGroup);
    //logInfof("groupInvite");
    //if (UserStatusManager.getInstance().getUserStatus(inviteGroup.destination))
    //{
    //  auto contexts = GateServerClient.getInstance.getAllClient();
    //  foreach(TransportSession s ; contexts)
    //  {
    //    s.send(new MessageBuffer(MESSAGE.CMD_INVITE_GROUP,inviteGroup.toProtobuf.array));
    //  }
    //}else
    //{
    //  MessageTransportClient logicClient = LogicServerClient.getInstance().getForwardClient();
    //  logicClient.send(new MessageBuffer(MESSAGE.CMD_OFFLINE_INVITE_GROUP,inviteGroup.toProtobuf.array));
    //}
  }

  @MessageId(MESSAGE.CMD_GROUP_MEMBER_CHANGE_HASH)
  void groupMemberChangeHash(TransportContext ctx, MessageBuffer buffer) {
    ChatMapAction action = new ChatMapAction;
    buffer.data.fromProtobuf!ChatMapAction(action);

    if (UserStatusManager.getInstance().getUserStatus(action.destination)) //如果在线向前端发送成员更新消息
    {
      TransportSession[ulong] contexts = GateServerClient.getInstance.getAllClient();
      foreach(TransportSession c ; contexts)
      {
        logInfo("updateGroupList  %s  ----  %s ---- %d" ,  action.destination , action.chatId , action.type);
        c.send(new MessageBuffer(MESSAGE.CMD_GROUP_MEMBER_CHANGE,action.toProtobuf.array));
      }
    }else
    {
      if (action.type == ChatMapActionType.ADD || action.type == ChatMapActionType.CREATE) //不在线拼接离线通知消息发送logic保存
      {
        if(action.member.length == 1 && action.manager == action.member[0])
        {
          return;
        }
        MessageTransportClient logicClient = LogicServerClient.getInstance().getForwardClient();
        string[] arr;
        foreach (string m; action.member)
        {
          if(m != action.manager)
          {
            arr ~= m;
          }
        }
        //foreach(string m ; action.member)
        //{
        auto msg = new Msg;
        msg.chatId = action.chatId;
        msg.fromUserId = action.manager;
        msg.message = arr.join(",");
        msg.msgTime = action.timestamp;
        if(action.type == ChatMapActionType.CREATE && action.member.length == 2)
        {
          msg.type = MsgType.NOTICE_CONTACTS_ADD;
        }else
        {
          msg.type = MsgType.NOTICE_GROUP_ADD;
        }

        msg.localId = action.destination;;
        logicClient.send(new MessageBuffer(MESSAGE.CMD_PUT_UNRECEIVE,msg.toProtobuf.array));
        //}
      }
      //if (action.type == ChatMapActionType.ADD || action.type == ChatMapActionType.CREATE)
      //{
      //  MessageTransportClient logicClient = LogicServerClient.getInstance().getForwardClient();
      //  foreach(string m ; action.member)
      //  {
      //    auto msg = new Msg;
      //    msg.chatId = action.chatId;
      //    msg.fromUserId = action.manager;
      //    msg.message = m;
      //    if(action.type == ChatMapActionType.CREATE && action.member.length == 2)
      //    {
      //      msg.type = MsgType.NOTICE_CONTACTS_ADD;
      //    }else
      //    {
      //      msg.type = MsgType.NOTICE_GROUP_ADD;
      //    }
      //    msg.localId = action.destination;
      //    logicClient.send(new MessageBuffer(MESSAGE.CMD_PUT_UNRECEIVE,msg.toProtobuf.array));
      //  }
      //}
    }
  }

   @MessageId(MESSAGE.CMD_SET_GROUP_NAME_HASH_RES)
   void setGroupNameHash(TransportContext ctx, MessageBuffer buffer) {

     auto res = new SetGroupNameRES;
     buffer.data.fromProtobuf!SetGroupNameRES(res);

     if (UserStatusManager.getInstance().getUserStatus(res.userId))
     {
       TransportSession[ulong] contexts = GateServerClient.getInstance.getAllClient();
       foreach(TransportSession c ; contexts)
       {
         c.send(new MessageBuffer(MESSAGE.CMD_SET_GROUP_NAME_RES,res.toProtobuf.array));
       }
     }
   }
}
